import { defineStore } from 'pinia';
import { getToken, removeToken, setToken } from '../utils/token-utils';
import type { UserInfoState } from './interface';
import {ElMessage} from 'element-plus'
import {staticRoutes} from '@/router/routes'
import { getUserInfoApi, loginApi, logoutApi } from '@/api/acl/login';


/**
 * 用户信息
 * @methods setUserInfos 设置用户信息
 */
export const useUserInfoStore = defineStore('userInfo', {

	state: (): UserInfoState => ({
    token: getToken() as string,
    name: '',
    avatar: '',
    menuRoutes: []
  }),

	actions: {
    async login (username: string, password: string) {
      const {token} = await loginApi(username,password)
      this.token = token
      setToken(token)
    },

    async getInfo () {
      const info = await getUserInfoApi()
      this.name = info.name
      this.avatar = info.avatar

      this.menuRoutes = staticRoutes

    },

    async reset () {
      // 如果当前是登陆的, 请求退出登陆
      if(this.name){
        await logoutApi()
      }
    },
	},
});
